/*
 * Copyright 2006 The Apache Software Foundation Licensed under the Apache
 * License, Version 2.0 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
 * or agreed to in writing, software distributed under the License is
 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */

package org.apache.ibatis.ibator.api.dom.java;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import org.apache.ibatis.ibator.api.dom.OutputUtilities;
import org.apache.ibatis.ibator.internal.util.StringUtility;

/**
 * @author Jeff Butler
 */
public class Interface extends JavaElement implements CompilationUnit
{
	private Set<FullyQualifiedJavaType> importedTypes;

	private FullyQualifiedJavaType type;

	private Set<FullyQualifiedJavaType> superInterfaceTypes;

	private List<Method> methods;

	private List<String> fileCommentLines;

	/**
     *  
     */
	public Interface(FullyQualifiedJavaType type)
	{
		super();
		this.type = type;
		superInterfaceTypes = new LinkedHashSet<FullyQualifiedJavaType>();
		methods = new ArrayList<Method>();
		importedTypes = new TreeSet<FullyQualifiedJavaType>();
		fileCommentLines = new ArrayList<String>();
	}

	public Interface(String type)
	{
		this(new FullyQualifiedJavaType(type));
	}

	public Set<FullyQualifiedJavaType> getImportedTypes()
	{
		return Collections.unmodifiableSet(importedTypes);
	}

	public void addImportedType(FullyQualifiedJavaType importedType)
	{
		if (importedType.isExplicitlyImported()
				&& !importedType.getPackageName().equals(type.getPackageName()))
		{
			importedTypes.add(importedType);
		}
	}

	public String getFormattedContent()
	{
		StringBuilder sb = new StringBuilder();

		for (String commentLine : fileCommentLines)
		{
			sb.append(commentLine);
			OutputUtilities.newLine(sb);
		}

		if (StringUtility.stringHasValue(getType().getPackageName()))
		{
			sb.append("package "); //$NON-NLS-1$
			sb.append(getType().getPackageName());
			sb.append(';');
			OutputUtilities.newLine(sb);
			OutputUtilities.newLine(sb);
		}

		Set<String> importStrings = OutputUtilities
				.calculateImports(importedTypes);
		for (String importString : importStrings)
		{
			sb.append(importString);
			OutputUtilities.newLine(sb);
		}

		if (importStrings.size() > 0)
		{
			OutputUtilities.newLine(sb);
		}

		int indentLevel = 0;

		addFormattedJavadoc(sb, indentLevel);
		addFormattedAnnotations(sb, indentLevel);

		sb.append(getVisibility().getValue());

		if (isStatic())
		{
			sb.append("static "); //$NON-NLS-1$
		}

		if (isFinal())
		{
			sb.append("final "); //$NON-NLS-1$
		}

		sb.append("interface "); //$NON-NLS-1$
		sb.append(getType().getShortName());

		if (getSuperInterfaceTypes().size() > 0)
		{
			sb.append(" extends "); //$NON-NLS-1$

			boolean comma = false;
			for (FullyQualifiedJavaType fqjt : getSuperInterfaceTypes())
			{
				if (comma)
				{
					sb.append(", "); //$NON-NLS-1$
				}
				else
				{
					comma = true;
				}

				sb.append(fqjt.getShortName());
			}
		}

		sb.append(" {"); //$NON-NLS-1$
		indentLevel++;

		Iterator<Method> mtdIter = getMethods().iterator();
		while (mtdIter.hasNext())
		{
			OutputUtilities.newLine(sb);
			Method method = mtdIter.next();
			sb.append(method.getFormattedContent(indentLevel, true));
			if (mtdIter.hasNext())
			{
				OutputUtilities.newLine(sb);
			}
		}

		indentLevel--;
		OutputUtilities.newLine(sb);
		OutputUtilities.javaIndent(sb, indentLevel);
		sb.append('}');

		return sb.toString();
	}

	public void addSuperInterface(FullyQualifiedJavaType superInterface)
	{
		superInterfaceTypes.add(superInterface);
	}

	/**
	 * @return Returns the methods.
	 */
	public List<Method> getMethods()
	{
		return methods;
	}

	public void addMethod(Method method)
	{
		methods.add(method);
	}

	/**
	 * @return Returns the type.
	 */
	public FullyQualifiedJavaType getType()
	{
		return type;
	}

	/*
	 * (non-Javadoc)
	 * @see
	 * org.apache.ibatis.ibator.internal.java.dom.CompilationUnit#getSuperClass
	 * ()
	 */
	public FullyQualifiedJavaType getSuperClass()
	{
		// interfaces do not have superclasses
		return null;
	}

	/*
	 * (non-Javadoc)
	 * @see org.apache.ibatis.ibator.internal.java.dom.CompilationUnit#
	 * getSuperInterfaceTypes()
	 */
	public Set<FullyQualifiedJavaType> getSuperInterfaceTypes()
	{
		return superInterfaceTypes;
	}

	/*
	 * (non-Javadoc)
	 * @see
	 * org.apache.ibatis.ibator.internal.java.dom.CompilationUnit#isJavaInterface
	 * ()
	 */
	public boolean isJavaInterface()
	{
		return true;
	}

	public boolean isJavaEnumeration()
	{
		return false;
	}

	public void addFileCommentLine(String commentLine)
	{
		fileCommentLines.add(commentLine);
	}

	public List<String> getFileCommentLines()
	{
		return fileCommentLines;
	}

	public void addImportedTypes(Set<FullyQualifiedJavaType> importedTypes)
	{
		this.importedTypes.addAll(importedTypes);
	}
}
